package com.xinghou.platform.server.api.controller;

import com.xinghou.platform.entity.req.ListReq;
import com.xinghou.platform.model.main.MessageContent;
import com.xinghou.platform.model.main.Notice;
import com.xinghou.platform.server.api.common.MessageContants;
import com.xinghou.platform.service.main.NoticeService;
import com.xinghou.platform.util.StringUtils;
import com.xinghou.platform.util.exception.DBExceptionEnums;
import com.xinghou.platform.util.exception.ServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by liufangyu on 16/7/8.
 */
@Controller
@SessionAttributes("token")
public class NoticeController extends  BaseControl {
    @Resource
    public NoticeService noticeService;


    /**
     * 话题列表
     * @param notice
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/notice",method = {RequestMethod.POST})
    public String notice( @RequestBody Notice notice,
                              HttpServletRequest request, HttpServletResponse response)
    {
    /*    if (StringUtils.isBlank(requestContent.get("uasystem")))
            throw new ServiceException(DBExceptionEnums.UA_SYSTEM_US_NULL);*/
        MessageContent content = new MessageContent();
        content.setJumppage(notice.getJumpNativePage()==null?"":notice.getJumpNativePage());
        content.setText(notice.getText()==null?"":notice.getText());
        content.setAttachid(notice.getAttachId()==null?"":notice.getAttachId());
        content.setUrl(notice.getUrl()==null?"":notice.getUrl());
        notice.setContent(content);
        notice.setMessagecode(MessageContants.SYSTEM_MESSAGE_CODE);
        noticeService.notice(notice);
        return response(null);
    }

    /**
     * 消息列表
     * @param listReq
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/noticeList",method = {RequestMethod.POST})
    public String getSubjects( @RequestBody ListReq listReq,
                               HttpServletRequest request, HttpServletResponse response)
    {
        List<Notice> list = noticeService.list(listReq);
        return response(list);

    }


    /**
     * 取消定时推送
     * @param notice
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/removeNotice",method = {RequestMethod.POST})
    public String removeNotice( @RequestBody Notice notice,
                               HttpServletRequest request, HttpServletResponse response)
    {
        if(StringUtils.isBlank(notice.getJpushid())) {
            throw new ServiceException(DBExceptionEnums.PRARM_ERROR);
        }
        noticeService.removeNotice(notice.getJpushid());
        return response(null);
    }



    /**
     * 修改定时推送
     * @param notice
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/updateNotice",method = {RequestMethod.POST})
    public String updateNotice( @RequestBody Notice notice,
                                HttpServletRequest request, HttpServletResponse response)
    {
        if(StringUtils.isBlank(notice.getJpushid())) {
            throw new ServiceException(DBExceptionEnums.PRARM_ERROR);
        }
        noticeService.updateNotice(notice);
        return response(null);
    }


    /**
     * 同城推送
     * @param notice
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/cityNotice",method = {RequestMethod.POST})
    public String cityNotice( @RequestBody Notice notice,
                          HttpServletRequest request, HttpServletResponse response)
    {
    /*    if (StringUtils.isBlank(requestContent.get("uasystem")))
            throw new ServiceException(DBExceptionEnums.UA_SYSTEM_US_NULL);*/
   /*     MessageContent content = new MessageContent();
        content.setJumppage(notice.getJumpNativePage()==null?"":notice.getJumpNativePage());
        content.setText(notice.getText()==null?"":notice.getText());
        content.setAttachid(notice.getAttachId()==null?"":notice.getAttachId());
        content.setUrl(notice.getUrl()==null?"":notice.getUrl());
        notice.setContent(content);*/
        notice.setMessagecode(MessageContants.SYSTEM_MESSAGE_CODE);
        noticeService.cityNotice(notice);
        return response(null);
    }

}
